package com.lanchetech.admin.controller;

import com.lanchetech.admin.aop.OperatorLog;
import com.lanchetech.admin.service.ProductService;
import com.lanchetech.bean.request.*;
import com.lanchetech.bean.response.BasePageResp;
import com.lanchetech.bean.response.BaseResp;
import com.lanchetech.bean.response.ResultData;
import com.lanchetech.bean.response.SpuResp;
import com.lanchetech.bean.vo.*;
import com.lanchetech.common.exception.ResultCodeException;
import com.lanchetech.entity.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Api(value = "商品接口", tags = "商品接口")
@RequestMapping("/api/product")
@RestController
public class ProductController {

    @Autowired
    ProductService productService;

    @ApiOperation("获取商品SPU分页")
    @PreAuthorize("hasAuthority('product.spu')")
    @PostMapping("/getSpuPage")
    public ResultData<BasePageResp<SpuResp>> getSpuPage(@RequestBody SpuReq req) {
        return productService.getSpuPage(req);
    }

    @ApiOperation("获取商品SPU和SKU列表分页")
    @PostMapping("/getProductPage")
    public ResultData<BasePageResp<SpuResp>> getProductPage(@RequestBody SpuReq req) {
        return productService.getProductPage(req);
    }

    @ApiOperation("是否为主打商品")
    @GetMapping("/preferredImage")
    public BaseResp preferredImage(@RequestParam Long spuId,
                                   @RequestParam boolean main) {
        return productService.preferredImage(spuId, main);
    }

    @OperatorLog("添加或编辑商品")
    @ApiOperation("添加或编辑商品")
    @PreAuthorize("hasAuthority('product.spu.edit.auth')")
    @PostMapping("/editSpuAndSku")
    public BaseResp editSpuAndSku(@RequestBody SpuEditVO spuEditVO) {
        return productService.editSpuAndSku(spuEditVO);
    }

    @OperatorLog("单独编辑商品")
    @ApiOperation("单独编辑商品")
    @PreAuthorize("hasAuthority('product.spu.edit.auth')")
    @PostMapping("/updateSpu")
    public BaseResp updateSpu(@RequestBody SpuEditVO spuEditVO) {
        BaseResp baseResp = new BaseResp();
        try {
            baseResp = productService.updateSpu(spuEditVO);
        } catch (ResultCodeException e) {
            baseResp.setResultCode(e.getResultCode());
        }
        return baseResp;
    }

    @OperatorLog("编辑商品排序")
    @ApiOperation("编辑商品排序")
    @PreAuthorize("hasAuthority('product.spu.edit.auth')")
    @PostMapping("/editSpuSort")
    public BaseResp editSpuSort(@RequestBody Spu req) {
        return productService.editSpuSort(req);
    }

    @ApiOperation("获取商品")
    @PreAuthorize("hasAuthority('product.spu-edit')")
    @GetMapping("/getSpuEditDetail")
    public ResultData<SpuEditVO> getSpuEditDetail(@RequestParam("id") Long id) {
        return productService.getSpuEditDetail(id);
    }

    @ApiOperation("获取销售属性分页")
    @PreAuthorize("hasAuthority('product.spec')")
    @PostMapping("/getSpecPage")
    public ResultData<BasePageResp<Spec>> getSpecPage(@RequestBody PageReq req) {
        return productService.getSpecPage(req);
    }

    @ApiOperation("获取销售属性列表")
    @GetMapping("/getSpecList")
    public ResultData<List<Spec>> getSpecList() {
        return productService.getSpecList();
    }

    @OperatorLog("添加或编辑销售属性")
    @ApiOperation("添加或编辑销售属性")
    @PreAuthorize("hasAuthority('product.spec.edit.auth')")
    @PostMapping("/editSpec")
    public BaseResp editSpec(@RequestBody Spec spec) {
        return productService.editSpec(spec);
    }

    @OperatorLog("删除销售属性")
    @ApiOperation("删除销售属性")
    @PreAuthorize("hasAuthority('product.spec.delete.auth')")
    @GetMapping("/deleteSpec")
    public BaseResp deleteSpec(@RequestParam("id") Long id) {
        return productService.deleteSpec(id);
    }

    @ApiOperation("获取单位分页")
    @PreAuthorize("hasAuthority('product.unit')")
    @PostMapping("/getUnitPage")
    public ResultData<BasePageResp<Unit>> getUnitPage(@RequestBody PageReq req) {
        return productService.getUnitPage(req);
    }

    @OperatorLog("添加或编辑单位")
    @ApiOperation("添加或编辑单位")
    @PreAuthorize("hasAuthority('product.unit.edit.auth')")
    @PostMapping("/editUnit")
    public BaseResp editUnit(@RequestBody Unit unit) {
        return productService.editUnit(unit);
    }

    @OperatorLog("删除单位")
    @ApiOperation("删除单位")
    @PreAuthorize("hasAuthority('product.unit.delete.auth')")
    @GetMapping("/deleteUnit")
    public BaseResp deleteUnit(@RequestParam("id") Long id) {
        return productService.deleteUnit(id);
    }

    @ApiOperation("获取单位列表")
    @GetMapping("/getUnitList")
    public ResultData<List<Unit>> getUnitList() {
        return productService.getUnitList();
    }

    @ApiOperation("获取运费模板分页")
    @PreAuthorize("hasAuthority('product.freight')")
    @PostMapping("/getFreightPage")
    public ResultData<BasePageResp<FreightTenantVO>> getFreightPage(@RequestBody PageReq req) {
        return productService.getFreightPage(req);
    }

    @OperatorLog("添加或编辑运费模板")
    @ApiOperation("添加或编辑运费模板")
    @PreAuthorize("hasAuthority('product.freight.edit.auth')")
    @PostMapping("/editFreight")
    public BaseResp editFreight(@RequestBody FreightVO freightVO) {
        return productService.editFreight(freightVO);
    }

    @ApiOperation("获取运费模板详情")
    @PreAuthorize("hasAuthority('product.freight-edit')")
    @GetMapping("/getFreightDetail")
    public ResultData<FreightVO> getFreightDetail(@RequestParam("id") Long id) {
        return productService.getFreightDetail(id);
    }

    @OperatorLog("删除运费模板")
    @ApiOperation("删除运费模板")
    @PreAuthorize("hasAuthority('product.freight.delete.auth')")
    @GetMapping("/deleteFreight")
    public BaseResp deleteFreight(@RequestParam("id") Long id) {
        return productService.deleteFreight(id);
    }

    @ApiOperation("获取运费模板列表")
    @GetMapping("/getFreightList")
    public ResultData<List<Freight>> getFreightList(@RequestParam("tenantId") Long tenantId) {
        return productService.getFreightList(tenantId);
    }

    @OperatorLog("删除商品")
    @ApiOperation("删除商品")
    @PreAuthorize("hasAuthority('product.spu.delete.auth')")
    @GetMapping("/deleteProduct")
    public BaseResp deleteProduct(@RequestParam("id") Long id) {
        return productService.deleteProduct(id);
    }

    @ApiOperation("获取商品SKU和佣金设置分页")
    @PreAuthorize("hasAuthority('product.sku-commission')")
    @PostMapping("/getSkuAndSkuCommissionPage")
    public ResultData<BasePageResp<SkuWithCommissionVO>> getSkuAndSkuCommissionPage(@RequestBody PageReq req) {
        return productService.getSkuAndSkuCommissionPage(req);
    }

    @OperatorLog("设置SKU佣金配置")
    @ApiOperation("设置SKU佣金配置")
    @PreAuthorize("hasAuthority('product.sku-commission.edit.auth')")
    @PostMapping("/editSkuCommission")
    public BaseResp editSkuCommission(@RequestBody SkuCommission req) {
        return productService.editSkuCommission(req);
    }

    @OperatorLog("删除SKU佣金配置")
    @ApiOperation("删除SKU佣金配置")
    @PreAuthorize("hasAuthority('product.sku-commission.edit.auth')")
    @GetMapping("/deleteSkuCommission")
    public BaseResp deleteSkuCommission(@RequestParam("id") Long id) {
        return productService.deleteSkuCommission(id);
    }

    @OperatorLog("设置商品快递收件费")
    @ApiOperation("设置商品快递收件费")
    @PreAuthorize("hasAuthority('product.spu.edit.auth')")
    @PostMapping("/editSpuCourierAmount")
    public BaseResp editSpuCourierAmount(@RequestBody SpuCourierAmount req) {
        return productService.editSpuCourierAmount(req);
    }

    @ApiOperation("获取商品快递收件费")
    @GetMapping("/getSpuCourierAmount")
    public ResultData<SpuCourierAmount> getSpuCourierAmount(@Param("spuId") Long spuId) {
        return productService.getSpuCourierAmount(spuId);
    }

    @ApiOperation("编辑专区")
    @PostMapping("/editZone")
    public BaseResp editZone(@RequestBody ZoneReq req) {
        return productService.editZone(req);
    }

    @ApiOperation("获取专区分页")
    @PostMapping("/getZonePage")
    public ResultData<BasePageResp<Zone>> getZonePage(@RequestBody ZonePageReq req) {
        return productService.getZonePage(req);
    }

    @ApiOperation("获取专区商品详情")
    @GetMapping("/getZoneSpuDetail")
    public ResultData<ZoneVO> getZoneSpuDetail(@RequestParam("zoneId") Long zoneId) {
        return productService.getZoneSpuDetail(zoneId);
    }

    @ApiOperation("删除专区")
    @GetMapping("/deleteZone")
    public BaseResp deleteZone(@RequestParam("zoneId") Long zoneId) {
        return productService.deleteZone(zoneId);
    }

    //---------------------------选品广场-------------------------------------------
    @ApiOperation("获取选品申请分页")
    @PostMapping("/getSelectionApplyPage")
    public ResultData<BasePageResp<SelectionApplyVO>> getSelectionApplyPage(@RequestBody PageReq req) {
        return productService.getSelectionApplyPage(req);
    }

    @ApiOperation("审核选品申请")
    @PostMapping("/auditSelectionApply")
    public BaseResp auditSelectionApply(@RequestBody SelectionApply req) {
        return productService.auditSelectionApply(req);
    }

    @ApiOperation("获取选品申请详情")
    @GetMapping("/getSelectionApplyDetail")
    public ResultData<SelectionApplyVO> getSelectionApplyDetail(@RequestParam("id") Long id) {
        return productService.getSelectionApplyDetail(id);
    }

    @ApiOperation("获取选品分页")
    @PostMapping("/getSelectionPage")
    public ResultData<BasePageResp<Selection>> getSelectionPage(@RequestBody PageReq req) {
        return productService.getSelectionPage(req);
    }

    @ApiOperation("审核选品")
    @PostMapping("/auditSelection")
    public BaseResp auditSelection(@RequestBody Selection req) {
        return productService.auditSelection(req);
    }

    //----------------------------------------------------------------------

    @ApiOperation(value = "导出选品分页Excel")
    @PostMapping("/exportSelectionExcel")
    public void exportSelectionExcel(@RequestBody PageReq req, HttpServletResponse response) throws IOException {
        productService.exportSelectionExcel(req, response);
    }

    @ApiOperation("批量上下架商品")
    @PostMapping("/batchOfflineProduct")
    public BaseResp batchOfflineProduct(@RequestBody BatchOfflineReq req) {
        return productService.batchOfflineProduct(req);
    }

    @ApiOperation("排序重置")
    @GetMapping("/resetSorts")
    public BaseResp resetSorts(@RequestParam("startNum") Long startNum, @RequestParam("endNum") Long endNum) {
        return productService.resetSorts(startNum, endNum);
    }

    @ApiOperation("商品审核")
    @GetMapping("/auditProduct")
    public BaseResp auditProduct(@RequestParam("spuId") Long spuId, @RequestParam("type") Byte type, @RequestParam(name = "message", required = false) String message) {
        return productService.auditProduct(spuId, type, message);
    }


    @ApiOperation("获取商品销量从高到低排名前10的商品")
    @GetMapping("/getTopSpu")
    public ResultData<List<SpuVO>> getTopSpu() {
        return productService.getTopSpu();
    }

    @ApiOperation("获取Boss圈")
    @GetMapping("/getBossRanking")
    public ResultData<BasePageResp<BossRankingVo>> getBossRanking(@RequestBody PageReq req) {
        return productService.getBossRanking(req);
    }
}
